package com.code.controller;

import com.alibaba.fastjson.JSONObject;
import com.code.domain.Activity;
import com.code.domain.Address;
import com.code.domain.Dish;
import com.code.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AddressController {
    @Autowired
    AddressService addressService;
    @RequestMapping("address/add")
    public Object addAddress(String addressName){
        JSONObject jsonObject=new JSONObject();
        if ( addressService.addAddress(addressName) >0 ){
            jsonObject.put("code" ,"添加成功");
        }else {
            jsonObject.put("code", "添加失败");
        }
        return jsonObject;
    }
    @RequestMapping("address/delete")
    public Object deleteAddress(Integer addressId){
        JSONObject jsonObject=new JSONObject();
        if ( addressService.deleteAddress(addressId) >0 ){
            jsonObject.put("code" ,"删除成功");
        }else {
            jsonObject.put("code", "删除失败");
        }
        return jsonObject;
    }
    @RequestMapping("address/search")
    public Object searchAddress(Integer addressId){
        JSONObject jsonObject=new JSONObject();
        Address address=addressService.search(addressId);
        if (  address!=null ){
            jsonObject.put("address" ,address);
        }else {
            jsonObject.put("address", "无结果,请检查id");
        }
        return jsonObject;
    }
    @RequestMapping("address/update")
    public Object updateAddress(Integer addressId,String addressName){
        JSONObject jsonObject=new JSONObject();
        Address address=addressService.search(addressId);
        if (address!=null){
            address=new Address(addressId,addressName);
            addressService.updateAddress(address);
            jsonObject.put("code" ,"更新成功");
        }else {
            jsonObject.put("code" ,"更新失败");
        }
        return jsonObject;
    }
    @RequestMapping("address/all")
    public Object allAddress (){
        JSONObject jsonObject=new JSONObject();
        List<Address> list=addressService.searchAll();
        if (list.isEmpty()){
            jsonObject=null;
        }else {
            jsonObject.put("list",list);
        }
        return jsonObject;
    }
}
